package com.cwj.user.configuration;

import com.cwj.user.fieter.cross_domain_filter;
import com.cwj.user.fieter.html_filter;
import com.cwj.user.fieter.userExists_filter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import javax.servlet.ServletContext;

/**
 * 注册filter到spring容器
 */
@Configuration
public class Filters {
    @Resource
    private com.cwj.user.dao.userDao userDao;
    @Resource
    private ServletContext servletContext;
        @Bean
        public FilterRegistrationBean<cross_domain_filter> cross_domain_filter(){
            cross_domain_filter filter = new cross_domain_filter();
            filter.setUrl("http://192.168.101.80");
            FilterRegistrationBean<cross_domain_filter> bean = new FilterRegistrationBean<>(filter);

            bean.addUrlPatterns("/*");
            return bean;
        }
        @Bean
        public FilterRegistrationBean<html_filter> html_filter(){
            html_filter filter = new html_filter();
            filter.setUserDao(userDao);
            filter.setServletContext(servletContext);
            FilterRegistrationBean<html_filter> bean = new FilterRegistrationBean<>(filter);
            bean.addUrlPatterns("*.html");
            return bean;
        }
        @Bean
        public FilterRegistrationBean<userExists_filter> userExistsFilter(){
            userExists_filter userExists_filter = new userExists_filter();
            userExists_filter.setUserDao(userDao);
            userExists_filter.setServletContext(servletContext);
            FilterRegistrationBean<com.cwj.user.fieter.userExists_filter> bean = new FilterRegistrationBean<>(userExists_filter);
            bean.addUrlPatterns("/welcome/*");
            return bean;
        }
}
